"""
弹窗处理方法
"""
from time import sleep

from selenium.webdriver.common.by import By

from base.base_page import BasePage
from utils import DriverUtil


class PopupPage(BasePage):
    """
    弹窗管理方法
    """

    def __init__(self):
        super().__init__(driver_type='web')
        self.driver = DriverUtil.get_web_driver()
        # 登录弹窗信息元素定位
        self.popup_page = By.CLASS_NAME, 'layui-layer-content'
        # 登录弹窗关闭元素定位
        self.close_btn = By.CLASS_NAME, 'layui-layer-close'
        # 登录弹窗确定元素定位
        self.determine_btn = By.LINK_TEXT, '确定'

    def login_popup_information(self):
        """
        获取登录页面弹窗信息
        """
        # info = self.find_element_method(self.popup_page).text
        try:
            sleep(1)
            info = self.find_element_method(self.popup_page).text
            if info:
                return info
        except:
            return self.driver.title

    def login_popup_close(self):
        """
        关闭登录页弹窗
        """
        self.click_element(self.find_element_method(self.close_btn))

    def login_popup_determine(self):
        """
        确定登录页弹窗
        """
        self.click_element(self.find_element_method(self.determine_btn))

    def js_popup_text(self):
        """
        获取js弹窗信息 (无法获取页面信息的弹窗)
        :return: 返回弹窗信息
        """
        return self.driver.switch_to.alert()

    def js_popup_accept(self):
        """
        接收js弹窗信息 (无法获取页面信息的弹窗)
        """
        self.driver.switch_to.alert().accept()

    def js_popup_dismiss(self):
        """
        关闭js弹窗信息 (无法获取页面信息的弹窗)

        """
        self.driver.switch_to.alert().dismiss()
